package com.mxl.service.cashorder.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.mxl.common.helper.OrderStatus;
import com.mxl.common.helper.OrderType;
import com.mxl.common.helper.ResponseErrorCode;
import com.mxl.common.helper.SequenceName;
import com.mxl.dao.cashorder.impl.ExchargeRuleDaoImpl;
import com.mxl.dao.cashorder.impl.GatewayPayOrderDaoImpl;
import com.mxl.dao.cashorder.impl.PurchaseOrderDaoImpl;
import com.mxl.dao.cashorder.impl.SequenceDaoImpl;
import com.mxl.dao.cashorder.model.ExchargeRule;
import com.mxl.dao.cashorder.model.GatewayPayOrder;
import com.mxl.dao.cashorder.model.PurchaseOrder;
import com.mxl.service.cashorder.GatewayPayOrderService;
import com.mxl.service.cashorder.SequenceService;
import com.mxl.service.cashorder.dto.CashOrderRequest;
import com.mxl.service.exception.AppServiceException;

/**
 * 保持订单和 卡密 一个事物里面
 * 
 * @author Administrator
 * 
 */
@Service("gatewayPayOrderService")
public class GatewayPayOrderServiceImpl implements GatewayPayOrderService {
	private static final Logger logger = LoggerFactory
			.getLogger(GatewayPayOrderServiceImpl.class);
	private static final int USE = 1;
	private static final int USE_NO = 0;

	private static final SimpleDateFormat dFormat = new SimpleDateFormat(
			"yyyyMMddHHmmss");

	@Resource(name = "exchargeRuleDao")
	private ExchargeRuleDaoImpl exchargeRuleDao;

	@Resource(name = "purchaseOrderDao")
	private PurchaseOrderDaoImpl purchaseOrderDao;

	@Resource(name = "sequenceService")
	private SequenceService sequenceService;

	@Resource(name = "gatewayPayOrderDao")
	// @Autowired
	// @Qualifier("gatewayPayOrderDao")
	private GatewayPayOrderDaoImpl gatewayPayOrderDao;

	public void saveGateWayOrder(CashOrderRequest request) {
		PurchaseOrder pOrder = wrapPurOrder(request);
		purchaseOrderDao.save(pOrder);
		GatewayPayOrder gOrder = wrapPayOrder(request);
		gOrder.setPid(pOrder.getId());
		gOrder.setSeqId(createSeqId());
		gOrder.setSeqNo(getSeqNo());
		gatewayPayOrderDao.save(gOrder);
		request.setSeqNo(gOrder.getSeqNo());
		request.setSeqId(gOrder.getSeqId());
	}

	public GatewayPayOrder queryBuSeqId(String seqId) {
		return gatewayPayOrderDao.get(seqId);
	}

	private GatewayPayOrder wrapPayOrder(CashOrderRequest request) {
		GatewayPayOrder order = new GatewayPayOrder();

		try {
			BeanUtils.copyProperties(request, order);
		} catch (Exception e) {
			logger.error("GatewayPayOrder wrapPayOrder fail........", e);
			throw new AppServiceException(
					ResponseErrorCode.GATEWAYORDER_WRAP_ERROR.getValue());
		}
		order.setOrderStatus(OrderStatus.INIT.getValue());
		order.setCreationDate(new Date());
		// 设置當前兑换率
		order.setRate(getMeberRate(request.getMemberCode(),
				request.getBossType(), request));
		return order;
	}

	private Long getMeberRate(String memberCode, int bossType,
			CashOrderRequest request) {
		ExchargeRule r = exchargeRuleDao.getRate(memberCode, bossType);
		if (r == null) {
			throw new AppServiceException(
					ResponseErrorCode.MEMBER_RATE_NOTEFFE.getValue());
		}
		return r.getRate();
	}

	/**
	 * 721+时间+SeqId
	 * 
	 * @return
	 */
	private String createSeqId() {
		String date = dFormat.format(new Date());
		return new StringBuilder()
				.append(OrderType.CARD.getValue())
				.append(date)
				.append(sequenceService
						.updateSeqNo(SequenceName.SEQ_GATEWAYPAYORDER_SEQID
								.getKey())).toString();
	}

	// 年月日时分秒+5位seq
	private String getSeqNo() {
		String date = dFormat.format(new Date());
		// chargeChannel.getSeqNextValue("SEQ_CHANNEL_ID");
		return new StringBuilder()
				.append(date)
				.append(sequenceService.updateSeqNo(SequenceName.SEQ_CHANNEL_ID
						.getKey())).toString();
	}

	private PurchaseOrder wrapPurOrder(CashOrderRequest request) {
		PurchaseOrder porder = new PurchaseOrder();
		porder.setBossType(request.getBossType());
		porder.setCardNo(request.getCardNo());
		porder.setCardPwd(request.getCardPwd());
		porder.setCardAmount(request.getSelectAmount());// / 直连直接是订单的金额
		porder.setCreationDate(new Date());
		porder.setState(USE);
		return porder;
	}
}
